
chrome.contextMenus.onClicked.addListener(genericOnClick);

// A generic onclick callback function.
function genericOnClick(info) {

    let  url = "";

    switch (info.menuItemId) {
        case 'link':
            console.log('linkUrl:', info.linkUrl);
            url=info.linkUrl;
            break;
        default:
            console.log('srcUrl:',info.srcUrl);
            url=info.srcUrl;
    }
    download(url)
}

//创建监听
chrome.runtime.onInstalled.addListener(function () {
    let contexts = [
        'link',
        'image',
        'video',
        'audio'
    ];
    for (let i = 0; i < contexts.length; i++) {
        let context = contexts[i];
        let title = "将("+context+")发送至IndSvr Download";
        chrome.contextMenus.create({
            title: title,
            contexts: [context],
            id: context
        });
    }
});
//创建下载监听
chrome.downloads.onCreated.addListener(function (res) {
    let finalUrl = res.finalUrl;
    let id = res.id;
    //暂停
    chrome.downloads.pause(id)
    //删除
    // chrome.downloads.cancel(id);
    download(finalUrl,id)
})


function download(url,id){
    let s = "/html/add_download.html?url="+btoa(unescape(encodeURIComponent(url)));
    if (id) {
        s = s+"&id="+id;
    }
    chrome.action.setPopup({popup:s})
    chrome.action.openPopup()
    chrome.action.setPopup({popup:"/html/popup.html"})
}